package cn.edu.nju.freshdelivery.dao.impl;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.nju.freshdelivery.dao.OrderDAO;
import cn.edu.nju.freshdelivery.model.Order;
import cn.edu.nju.freshdelivery.model.Seller;

/**
 * A data access object (DAO) providing persistence and search support for Order
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see cn.edu.nju.freshdelivery.dao.impl.Order
 * @author MyEclipse Persistence Tools
 */

public class OrderDAOImpl extends HibernateDaoSupport implements OrderDAO {
	private static final Logger log = LoggerFactory
			.getLogger(OrderDAOImpl.class);
	// property constants
	public static final String STATE = "state";
	public static final String COST = "cost";
	public static final String NAME = "name";
	public static final String ADDRESS = "address";
	public static final String PHONE = "phone";
	public static final String BUYER = "buyer";
	public static final String SELLER = "seller";

	protected void initDao() {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#save(cn.edu.nju.freshdelivery
	 * .model.Order)
	 */
	public void save(Order transientInstance) {
		log.debug("saving Order instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#delete(cn.edu.nju.freshdelivery
	 * .model.Order)
	 */
	public void delete(Order persistentInstance) {
		log.debug("deleting Order instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findById(java.lang.Integer)
	 */
	public Order findById(java.lang.Integer id) {
		log.debug("getting Order instance with id: " + id);
		try {
			Order instance = (Order) getHibernateTemplate().get(
					"cn.edu.nju.freshdelivery.model.Order", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByExample(cn.edu.nju.
	 * freshdelivery.model.Order)
	 */
	public List<Order> findByExample(Order instance) {
		log.debug("finding Order instance by example");
		try {
			List<Order> results = (List<Order>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByProperty(java.lang.String
	 * , java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Order instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Order as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByState(java.lang.Object)
	 */
	public List<Order> findByState(Object state) {
		return findByProperty(STATE, state);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByCost(java.lang.Object)
	 */
	public List<Order> findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByName(java.lang.Object)
	 */
	public List<Order> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByAddress(java.lang.Object
	 * )
	 */
	public List<Order> findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findByPhone(java.lang.Object)
	 */
	public List<Order> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.freshdelivery.dao.impl.OrderDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Order instances");
		try {
			String queryString = "from Order";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.OrderDAO#merge(cn.edu.nju.freshdelivery
	 * .model.Order)
	 */
	public Order merge(Order detachedInstance) {
		log.debug("merging Order instance");
		try {
			Order result = (Order) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Order instance) {
		log.debug("attaching dirty Order instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Order instance) {
		log.debug("attaching clean Order instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static OrderDAO getFromApplicationContext(ApplicationContext ctx) {
		return (OrderDAO) ctx.getBean("OrderDAO");
	}

	@Override
	public List<Order> findByBuyer(Object buyer) {
		return findByProperty(BUYER, buyer);
	}

	@Override
	public List<Order> findBySeller(Seller seller) {
		return findByProperty(SELLER, seller);
	}
}